﻿using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using Application = Microsoft.Office.Interop.Word.Application;

namespace Report.Service
{
    public class WordProcessor
    {
        private Application appWord;
        private Document doc;

        public void Prepare()
        {
            appWord = new Application();
        }

        public void LoadTemplateDocument(string templateDocument)
        {
            doc = new Document();
            object objTemplate = templateDocument;
            object objDocType = WdDocumentType.wdTypeDocument;
            object objFalse = false;
            object objTrue = true;
            doc = appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue);
        }

        public void ReplaceImage(string bookMarkName, string imagePath)
        {
            object obDdName = bookMarkName;
            doc.Bookmarks.get_Item(ref obDdName).Range.Select();
            Clipboard.SetImage(Image.FromFile(imagePath));
            doc.Bookmarks.get_Item(ref obDdName).Range.Paste();
        }

        public void SetTableCell(string bookMarkName, int x, int y, string value)
        {
            object obDdName = bookMarkName;
            Table table = doc.Bookmarks.get_Item(ref obDdName).Range.Tables[1];
            if (table.Rows.Count < x)
            {
                object beforeRow = Type.Missing;
                table.Rows.Add(ref beforeRow);
            }
            table.Cell(x, y).Range.Text = value;
        }

        public void SaveAndQuit(string fileName)
        {
            object filename = fileName;
            object miss = Missing.Value;
            doc.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss,
                       ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
            object missingValue = Type.Missing;
            object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
            doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
            appWord.Application.Quit(ref miss, ref miss, ref miss);
        }

        public void SaveAndShow(string fileName)
        {
            object filename = fileName;
            object miss = Missing.Value;
            doc.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss,
                       ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
            appWord.Visible = true;
            appWord.ShowMe();
        }

        public void DeleteTableRow(string bookMarkName, int startIndex)
        {
            object obDdName = bookMarkName;
            Table table = doc.Bookmarks.get_Item(ref obDdName).Range.Tables[1];

            for (int i = startIndex; i <= table.Rows.Count; i++)
            {
                for(int j=1;j<=table.Columns.Count;j++)
                {
                    table.Cell(i, j).Range.Text = "";
                }
            }
        }
    }
}